home *** CD-ROM | disk | FTP | other *** search
/ Garbo / Garbo.cdr / mac / source / dialgmgr.sit / Dialogs ƒ / ListItem.p < prev    next >
Text File  |  1989-11-29  |  6KB  |  270 lines

  1. unit TListItem;
  2.  
  3. {        ⌐1986-1989            Bill Stackhouse                                    }
  4. {                                Stackhouse Software                                }
  5. {                                Natick, MA 01760                                    }
  6.  
  7. interface
  8.  
  9. {$IFC UNDEFINED UseListItem}
  10. {$SETC UseListItem = FALSE}
  11. {$ENDC}
  12. {$IFC UseListItem}
  13.  
  14.     uses
  15.         TObject, TStringDB;
  16.  
  17.     const
  18.         maxListItems = 10;
  19.  
  20.     type
  21.         TListItem = object(TObject)
  22.                 numListItems: 0..maxListItems;                {number of there list items}
  23.                 ListItem: array[1..maxListItems] of record
  24.                         num: Integer;                    {number of item with this box}
  25.                         numWords: Integer;            {}
  26.                         maxWords: Integer;            {}
  27.                         initiallyOn: Integer;            {}
  28.                         wordsShown: Integer;        {}
  29.                         box: Rect;                        {}
  30.                         eventBox: Rect;                {}
  31.                         updateRgn: RgnHandle;        {}
  32.                         curList: ListHandle;            {}
  33.                         words: TStringDB;
  34.                     end;
  35.                 procedure TListItem.Init;
  36.                 procedure TListItem.Add (pItem: Integer;
  37.                                             pInitiallyOn: Integer;
  38.                                             pWordsShown: Integer);
  39.                 procedure TListItem.AddItem (pItemText: Str255);
  40.                 procedure TListItem.Revise (curDialog: DialogPtr);
  41.                 procedure TListItem.Draw (i: Integer);
  42.                 procedure TListItem.DrawAll;
  43.                 procedure TListItem.Mouse (curDialog: DialogPtr;
  44.                                             theEvent: EventRecord);
  45.                 procedure TListItem.Get (theGroup: Integer;
  46.                                             var theItem: Integer;
  47.                                             var theText: Str255);
  48.                 function TListItem.Error: Integer;
  49.                 procedure TListItem.Free;
  50.                 override;
  51.             end;
  52.  
  53. {$ENDC}
  54.  
  55. implementation
  56.  
  57. {$IFC UseListItem}
  58.  
  59.     const
  60.         Off = 0;
  61.         On = 1;
  62.  
  63.         btnCtrlItem = 4;
  64.         chkCtrlItem = 5;
  65.         radCtrlItem = 6;
  66.         editCtrlItem = 16;
  67.  
  68.         DialogGroupIgnored = -10;    {too many groups, key, menus, or user items were added}
  69.  
  70.     type
  71.         TSearch = (searching, found, endList);
  72.  
  73.     var
  74.         globalError: Integer;
  75.  
  76.     procedure TListItem.Init;
  77.     begin
  78.         globalError := noErr;
  79.         SELF.numListItems := 0;
  80.     end;        {TListItem.Init}
  81.  
  82.     procedure TListItem.Add (pItem: Integer;
  83.                                     pInitiallyOn: Integer;
  84.                                     pWordsShown: Integer);
  85.     begin
  86.         globalError := noErr;
  87.         if pInitiallyOn < 1 then
  88.             pInitiallyOn := 1;
  89.         if pWordsShown < 1 then
  90.             pWordsShown := 1;
  91.         with SELF do
  92.             begin
  93.                 if numListItems < maxListItems then
  94.                     begin
  95.                         numListItems := numListItems + 1;
  96.                         with ListItem[numListItems] do
  97.                             begin
  98.                                 num := pItem;
  99.                                 initiallyOn := pInitiallyOn;
  100.                                 wordsShown := pWordsShown;
  101.                                 curList := nil;
  102.                                 new(words);
  103.                                 words.Init;
  104.                             end;    {with}
  105.                     end    {if}
  106.                 else
  107.                     globalError := DialogGroupIgnored;
  108.             end;
  109.     end;        {TListItem.Add}
  110.  
  111.     procedure TListItem.AddItem (pItemText: Str255);
  112.     begin
  113.         globalError := noErr;
  114.         SELF.ListItem[numListItems].words.Add(pItemText);
  115.         if MemError <> noErr then
  116.             globalError := DialogGroupIgnored;
  117.     end;        {TListItem.AddItem}
  118.  
  119.     procedure TListItem.Revise (curDialog: DialogPtr);
  120.         var
  121.             i, j: Integer;
  122.             theType: Integer;
  123.             theHandle: Handle;
  124.     begin
  125.         globalError := noErr;
  126.         for i := 1 to SELF.numListItems do
  127.             with SELF.ListItem[i] do
  128.                 begin
  129.                     GetDItem(curDialog, num, theType, theHandle, box);
  130.                     updateRgn := NewRgn;
  131.                     OpenRgn;
  132.                     FrameRect(box);
  133.                     CloseRgn(updateRgn);
  134.                 end;
  135.     end;        {TListItem.Revise}
  136.  
  137.     procedure TListItem.Draw (i: Integer);
  138.         var
  139.             j: Integer;
  140.             dataBounds: Rect;                {the following are all passed to NewList}
  141.             cSize: Point;
  142.             theProc: Integer;
  143.             drawIt: Boolean;
  144.             hasGrow: Boolean;
  145.             scrollHoriz: Boolean;
  146.             scrollVert: Boolean;
  147.             outlineBox: Rect;
  148.             word: Str255;
  149.             theFont: FontInfo;                {info about the font used in the menu}
  150.     begin
  151.         globalError := noErr;
  152.         with SELF.ListItem[i] do
  153.             begin
  154.                 if curList = nil then
  155.                     begin
  156.                         GetFontInfo(theFont);
  157.                         with theFont, box do
  158.                             SetRect(box, left, top, right, top + (wordsShown * (ascent + descent + leading))); {l,t,r,b}
  159.                         eventBox := box;
  160.                         SetRect(dataBounds, 0, 0, 1, words.Count);
  161.                         cSize.h := 0;
  162.                         cSize.v := 0;
  163.                         theProc := 0;
  164.                         drawIt := TRUE;
  165.                         hasGrow := FALSE;
  166.                         scrollHoriz := FALSE;
  167.                         scrollVert := (words.Count > wordsShown);
  168.                         if scrollVert then
  169.                             with box do
  170.                                 SetRect(box, left, top, right - 16, bottom);
  171.                         curList := LNew(box, dataBounds, cSize, theProc, FrontWindow, drawIt, hasGrow, scrollHoriz, scrollVert);
  172.                         for j := 1 to words.Count do
  173.                             begin
  174.                                 cSize.h := 0;
  175.                                 cSize.v := j - 1;
  176.                                 if j = 1 then
  177.                                     word := words.ReadFirst
  178.                                 else
  179.                                     word := words.ReadNext;
  180.                                 LSetCell(Pointer(Longint(@word) + 1), Length(word), cSize, curList);
  181.                             end;    {for}
  182.                         cSize.h := 0;
  183.                         cSize.v := initiallyOn - 1;
  184.                         LSetSelect(TRUE, cSize, curList);
  185.                         LAutoScroll(curList);
  186.                     end;        {if nil}
  187.                 LUpdate(updateRgn, curList);
  188.                 outlinebox := box;
  189.                 InsetRect(outlineBox, -1, -1);
  190.                 FrameRect(outlineBox);
  191.             end;
  192.     end;        {TListItem.Draw}
  193.  
  194.     procedure TListItem.DrawAll;
  195.         var
  196.             i: Integer;
  197.     begin
  198.         globalError := noErr;
  199.         for i := 1 to numListItems do
  200.             SELF.Draw(i);
  201.     end;        {TListItem.DrawAll}
  202.  
  203.     procedure TListItem.Mouse (curDialog: DialogPtr;
  204.                                     theEvent: EventRecord);
  205.         var
  206.             i: Integer;
  207.     begin
  208.         globalError := noErr;
  209.         GlobalToLocal(theEvent.where);                            {because PtInRect wants it that way}
  210.         with SELF do
  211.             begin
  212.                 if numListItems > 0 then
  213.                     for i := 1 to numListItems do
  214.                         with ListItem[i] do
  215.                             if PtInRect(theEvent.where, eventBox) then
  216.                                 begin
  217.                                     with theEvent do
  218.                                         if LClick(where, modifiers, curList) then
  219.                                             ;
  220.                                     Leave;
  221.                                 end;            {if PtInRect}
  222.             end;    {With}
  223.     end;        {TListItem.Mouse}
  224.  
  225.     procedure TListItem.Get (theGroup: Integer;
  226.                                     var theItem: Integer;
  227.                                     var theText: Str255);
  228.         var
  229.             theCell: Point;
  230.             itemTextSize: Integer;
  231.     begin
  232.         globalError := noErr;
  233.         with SELF.Listitem[theGroup] do
  234.             begin
  235.                 theCell := LLastClick(curList);
  236.                 if theCell.v < 0 then
  237.                     begin
  238.                         theCell.h := 0;
  239.                         theCell.v := initiallyOn - 1;
  240.                     end;
  241.                 itemTextSize := 255;
  242.                 LGetCell(Ptr(Longint(@theText) + 1), itemTextSize, theCell, curList);
  243. {$R-}
  244.                 theText[0] := CHR(itemTextSize);
  245. {$R+}
  246.                 theItem := theCell.v + 1;
  247.             end;
  248.     end;        {TListItem.Get}
  249.  
  250.     function TListItem.Error: Integer;
  251.     begin
  252.         Error := globalError;
  253.     end;        {TListItem.Error}
  254.  
  255.     procedure TListItem.Free;
  256.         var
  257.             i: Integer;
  258.     begin
  259.         globalError := noErr;
  260.         for i := 1 to SELF.numListItems do
  261.             begin
  262.                 LDispose(SELF.Listitem[i].curlist);
  263.                 DisposeRgn(SELF.Listitem[i].updateRgn);
  264.             end;
  265.         inherited Free;
  266.     end;        {TListItem.Free}
  267.  
  268. {$ENDC}
  269.  
  270. end.